package com.financing.wap.service.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.financing.wap.common.Pager;
import com.financing.wap.dao.IProjectLikeDao;
import com.financing.wap.dto.ProjectLikeDTO;
import com.financing.wap.service.IProjectLikeService;
import com.financing.wap.vo.ProjectLikeVO;
import com.financing.wap.vo.ProjectSubVO;


@Service("projectLikeServiceImpl")
public class ProjectLikeServiceImpl implements IProjectLikeService{

	@Autowired
	private IProjectLikeDao projectLikeDaoImpl;
	
	@Override
	public List<ProjectLikeVO> queryProjectLike(ProjectLikeDTO projectLikeDTO,Pager pager)
			throws SQLException {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("projectLikeDTO", projectLikeDTO);
		if(pager != null){
			param.put("pager", pager);
		}
		return projectLikeDaoImpl.projectLikeList(param);
	}

	@Override
	public int addProjectLike(ProjectLikeDTO projectLikeDTO)
			throws SQLException {
		return projectLikeDaoImpl.addProjectLike(projectLikeDTO);
	}

	@Override
	public int deleteProjectLike(ProjectLikeDTO projectLikeDTO)
			throws SQLException {
		return projectLikeDaoImpl.deleteProjectLike(projectLikeDTO);
	}

	@Override
	public int countProjectLike(Integer projectId) {
		return projectLikeDaoImpl.countProjectLike(projectId);
	}

	@Override
	public int countProjectLikeForFa(ProjectLikeDTO projectLikeDTO) {
		return projectLikeDaoImpl.countProjectLikeForFa(projectLikeDTO);
	}
	
	@Override
	public int countProjectLikepType(Map<String, Object> map) {
		return projectLikeDaoImpl.countProjectLikepType(map);
	}
	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public ProjectLikeDTO queryProjectLikeType(Map<String, Object> map) throws Exception {
		List<ProjectLikeDTO> list = projectLikeDaoImpl.queryProjectLikeType(map);
		
		  if(list!=null && list.size()>0){
			  
		   for (int i = 0; i < list.size(); i++) {
			   if(i==0){
				   continue;
				   }
			   ProjectLikeDTO projectLikeDTO=list.get(i);
			   projectLikeDaoImpl.deleteProjectLike(projectLikeDTO);
		   				}
		   return list.get(0);
	   }
		
		return null;
	}

	@Override
	public List<ProjectSubVO> queryProjectSubFlow(List<ProjectLikeVO> projectList, Pager pager) {
		Map<String,Object> param = new HashMap<String,Object>();
		if(pager != null){
			param.put("pager", pager);
		}
		// 拼接所有订阅的产品pId
		StringBuffer buffer = new StringBuffer();
		for (ProjectLikeVO projectLikeDTO : projectList) {
			buffer.append(projectLikeDTO.getpId()).append(",");
		}
		param.put("pIds", buffer.toString());
		// 查询订阅产品对应的 订阅通知
		List<ProjectSubVO> list = projectLikeDaoImpl.queryProjectFlow(param);
		// 查询产品的订阅数
		for (ProjectSubVO projectSubVO : list) {
			projectSubVO.setSubTimes(projectLikeDaoImpl.querySubTimesBypId(projectSubVO.getpId()));
		}
		return  list;
	}

	@Override
	public List<ProjectLikeVO> queryProjectSubForFa(String uId) {
		return projectLikeDaoImpl.queryProjectSubForFa(uId);
	}

	@Override
	public int countProjectFlow(Integer pId) {
		return projectLikeDaoImpl.countProjectFlow(pId);
	}

	@Override
	public ProjectSubVO queryProjectFlowById(Map<String, Object> map) {
		return projectLikeDaoImpl.queryProjectFlow(map).get(0);
	}

	@Override
	public Date getPrijectLikeUpdateDateByFa(String id) throws SQLException {
		// 查询订阅产品
		List<ProjectLikeVO> projectList = this.queryProjectSubForFa(id);
		// 拼接所有订阅的产品pId
		Map<String,Object> param = new HashMap<String,Object>();
		StringBuffer buffer = new StringBuffer();
		for (ProjectLikeVO projectLikeDTO : projectList) {
			buffer.append(projectLikeDTO.getpId()).append(",");
		}
		param.put("pIds", buffer.toString());
		// 查询对应信息
		return projectLikeDaoImpl.getPrijectLikeUpdateDateByFa(param);
	}

}
